﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Koonsoft.HYCRM.BLL;
using Koonsoft.HYCRM.Model;
using Koonsoft.HYCRM.Model.Enum;

public partial class MyEmplyeeContract : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.QueryString["ID"] != null)
            {
                BindEmpolyeeData(Convert.ToInt32(Request.QueryString["ID"]));
                Button1.Visible = false;
            }
            else
            {
                BindEmpolyeeData(new EmpolyeeBLL().GetEmpolyeeInfoByLoginName(Page.User.Identity.Name).EmpolyeeID);
            }
        }

    }
    private void BindEmpolyeeData(int id)
    {
        int empolyeeID = id;

        var empolyee = new EmpolyeeBLL().GetEmpolyeeInfoByEmpolyeeID(empolyeeID);

        if (empolyee != null)
        {
            try
            {
                EmpolyeeName.Text = empolyee.Name;
                LoginName.Text = empolyee.LoginName;
                txtEntryTime.Text = empolyee.EntryTime.ToString("yyyy-MM-dd");
                txtBirthday.Text = empolyee.Expansion.Birthday.ToString("yyyy-MM-dd");
                txtPhone.Text = empolyee.Expansion.Phone;
                txtAddress.Text = empolyee.Expansion.Address;
                txtIdCard.Text = empolyee.Expansion.IdCard;
                txtStatus.Text = RemarkAttribute.GetEnumRemark((EmpolyeeStatus)empolyee.Status);
                labBasicWage.Text = empolyee.BasicWage.ToString();
                labLivingSubsidy.Text = empolyee.LivingSubsidy.ToString();
                labConfidential.Text = empolyee.Confidential.ToString();
                labAll.Text = empolyee.Other.ToString();
                labOther.Text =
                    (empolyee.Other - empolyee.Confidential - empolyee.LivingSubsidy - empolyee.BasicWage).ToString();

                txtGender.Text = empolyee.Expansion.Sex == 1 ? "男" : "女";
                txtAge.Text = EmpolyeeBLL.CalculateAge(empolyee.Expansion.Birthday, DateTime.Now).ToString();
            }
            catch (Exception)
            {
                if (Request.QueryString["ID"] == null)
                {
                    AlterAndRedirect("请补录信息", "bulu.aspx");
                }
                return;
            }

        }
        try
        {
            var contracts = new EmpolyeeBLL().GetEmplyeeContractByEmplyeeId(empolyeeID);


            if ((contracts.Count(fun => fun.Status == 1) < 1 ||
                 contracts.Where(fun => fun.Status == 1).OrderByDescending(fun => fun.EndTime).First().EndTime <
                 DateTime.Now.AddMonths(1))
                && empolyee.EntryTime <= DateTime.Now)
            {
                ContractDiv.Visible = true;
            }
            else
            {
                ContractDiv.Visible = false;
            }

            if (contracts.Count(fun => fun.Status == 1 || fun.Status == 2) < 1 &&
                empolyee.EntryTime > DateTime.Now.AddMonths(-3))
            {
                DropDownList1.Items.Add(new ListItem("试用期[3个月]", "3"));
            }
            else
            {
                for (int i = 1; i <= 5 && i <= 65 - (DateTime.Now.Year - empolyee.Expansion.Birthday.Year); i++)
                {
                    DropDownList1.Items.Add(new ListItem(string.Format("{0}年", i), string.Format("{0}", i * 12)));
                }
            }



            GridView1.DataSource = contracts;
            GridView1.DataBind();

            GridView2.DataSource = new EmpolyeeDAL().GetChangeEmpolyeeWageLogByEmplyeeId(empolyeeID);
            GridView2.DataBind();
        }
        catch (Exception)
        {

            if (Request.QueryString["ID"] == null)
            {
                AlterAndRedirect("请补录信息", "bulu.aspx");
            }
            return;
        }
    }


    /// <summary>
    /// 行绑定事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            int status = Convert.ToInt32(e.Row.Cells[3].Text);
            EmplyeeContractStatus pStatus = (EmplyeeContractStatus)status;
            e.Row.Cells[3].Text = RemarkAttribute.GetEnumRemark(pStatus);


            //LinkButton LinkContract = e.Row.FindControl("LinkContract") as LinkButton;


            //if (pStatus == EmplyeeContractStatus.Create)
            //{
            //    LinkContract.Visible = true;
            //}

        }
    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        int id = Convert.ToInt32(e.CommandArgument.ToString());

        switch (e.CommandName)
        {
            case "Contract":
                if (new EmpolyeeBLL().ConfirmEmplyeeContract(id))
                {
                    AlertMsg("操作成功");
                }
                else
                {
                    AlertMsg("操作失败");
                }
                break;
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        int empolyeeID = new EmpolyeeBLL().GetEmpolyeeInfoByLoginName(Page.User.Identity.Name).EmpolyeeID;
        if (new EmpolyeeBLL().ConfirmEmplyeeContract(empolyeeID, Convert.ToInt32(DropDownList1.SelectedValue), Page.Server.MapPath("~/Upload/")))
        {
            Button1.Enabled = false;
            AlertMsg("签署成功");
        }
        else
        {
            AlertMsg("操作失败");
        }
    }
}